package com.homegoods.login;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.homegoods.classify.model.Constant;
import com.homegoods.login.model.AuthCodeLoginParams;
import com.homegoods.login.model.ExternalUserModel;
import com.homegoods.login.model.RegisterParams;
import com.homegoods.login.model.UserModel;
import com.homegoods.login.model.UserParams;
import com.network.retrofit_rx.Api.ActionSubscriber;
import com.network.retrofit_rx.Api.CommandImpl;
import com.network.retrofit_rx.Api.HttpManager;

import java.util.Map;

import io.reactivex.functions.Consumer;

/**
 * @author QiQi
 * @ClassName: ApiUtil
 * @date 2018/9/13
 * @description
 */
public class ApiUtil {
    /**
     * 用户密码登录
     * <p>
     * UserName=kedll2019 //账号
     * PassWord=123456                        //密码
     *
     * @param consumer
     */
    //del shibo.zheng start
    /*public static void userPwdLogin(String userName, String pwd, Consumer<UserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_PWDLOGIN)
                .addParams("UserName", userName)
                .addParams("PassWord", pwd)
                .setObject(true)
                .setClass(UserModel.class)
                .create();
        HttpManager.getInstance().post(command, new ActionSubscriber<>(consumer, error, null));
    }*/
    //del shibo.zheng end
    //add shibo.zheng start
    public static void userPwdLogin(UserParams userParams/*Map<String, Object> mapParams*/, Consumer<UserModel> consumer, Consumer<Throwable> error) {
        /*MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody body = RequestBody.create(mediaType, com.alibaba.fastjson.JSON.toJSONString(userParams));
        Request request = new Request.Builder().url(Constant.SERVER_URL + Constant.CLIENT_PWDLOGIN).post(body).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("zhengshibo", "onFailure: ================");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = body1.string();
                Log.d("zhengshibo", "onResponse: body == " + body1 + "  body == " + JSON.toJSONString(body1) + " body " + string);
            }
        });*/

        HttpManager.getInstance().post(Constant.SERVER_URL + Constant.CLIENT_PWDLOGIN,
                JSON.toJSONString(userParams),
                new ActionSubscriber<>(consumer, error, null),  //回调
                UserModel.class);
    }
    //add shibo.zheng end


    /**
     * 用户手机号登录
     * <p>
     * MoblePhone=15800327113                     //手机号
     * ValidCode=435678                           //验证码
     * PassWord=123456                //密码  注册时可选项
     *
     * @param consumer
     */
    public static void userPhoneLogin(AuthCodeLoginParams params, Consumer<UserModel> consumer, Consumer<Throwable> error) {
        /*CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_PHONELOGIN)
                .setObject(true)
                .setClass(UserModel.class)
                .create();*/
        HttpManager.getInstance().post(Constant.SERVER_URL + Constant.CLIENT_AUTHLOGIN,
                JSON.toJSONString(params),
                new ActionSubscriber<>(consumer, error, null),
                UserModel.class);
    }
    /*public static void userPhoneLogin(String MoblePhone, String ValidCode, Consumer<UserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_PHONELOGIN)
                .addField("MoblePhone", MoblePhone)
                .addField("ValidCode", ValidCode)
                .setObject(true)
                .setClass(UserModel.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }*/

    /**
     * 用户注册
     * <p>
     * MoblePhone=15800327113                     //手机号
     * ValidCode=435678                           //验证码
     * PassWord=123456                //密码  注册时可选项
     *
     * @return
     */
    //del shibo.zheng start
    /*public static void registerUser(String phone, String code, String pwd, Consumer<String> consumer) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_PHONELOGIN)
                .addParams("MoblePhone", phone)
                .addParams("ValidCode", code)
                .addParams("PassWord", pwd)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().post(command, new ActionSubscriber<>(consumer, null, null));
    }*/
    //del shibo.zheng end
    //add shibo.zheng start
    public static void registerUser(RegisterParams params, Consumer<String> consumer, Consumer<Throwable> throwable) {
        /*CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_PHONELOGIN)
                .addParams("telephone", phone)
                .addParams("authCode", code)
                .addParams("password", pwd)
                .setObject(true)
                .setClass(String.class)
                .create();*/
        HttpManager.getInstance().post(Constant.SERVER_URL + Constant.CLIENT_PHONELOGIN,
                JSON.toJSONString(params),
                new ActionSubscriber<>(consumer, throwable, null),
                String.class);
    }
    //add shibo.zheng end

    /**
     * 获取短信验证码(注册用)
     *
     * @param phone
     * @return
     */
    //del shibo.zheng start
    /*public static void getCaptcha(String phone, Consumer<JSONObject> consumer) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_SMS)
                .addParams("mphone", phone)
                .addParams("job", "reg")
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, null, null));
    }*/
    //del shibo.zheng end
    //add shibo.zheng start
    public static void getCaptcha(String phone, Consumer<JSONObject> consumer) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_SMS)
                .addParams("telephone", phone)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, null, null));
    }
    //add shibo.zheng end


    /**
     * 获取短信验证码(已经注册后使用)
     *
     * @param phone
     * @return
     */
    public static void getCaptchaForForgetPassWord(String phone, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_SMS)
                .addParams("telephone", phone)
//                .addParams("job", "login")
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }


    /**
     * 忘记密码
     *
     * @param params
     * @param consumer MoblePhone=15800327113                     //手机号
     *                 ValidCode=435678                           //验证码
     *                 PassWord=123456                            //新密码
     */
    public static void forgetPassWord(Map<String, Object> params, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.RESET_PWD)
                .putParams(params)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postForBoolean(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 微信登录
     * outway=json                           //输出方式
     * code=0618VsGL0xMuP92wluFL0HebGL08VsGE //临时CODE必选
     * echostr=                              //回显字符
     * signature=
     * timestamp=
     * nonce=
     */
    //del shibo.zheng start
    /*public static void weChatLogin(String code, Consumer<ExternalUserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_WECHATLOGIN)
                .addParams("outway", "json")
                .addParams("code", code)
                .setObject(true)
                .setClass(ExternalUserModel.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }*/
    //del shibo.zheng end

    //add shibo.zheng start
    public static void weChatLogin(String code, Consumer<ExternalUserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_WECHATLOGIN)
                .addParams("provider", "wechat")
                .addParams("code", code)
                .setObject(true)
                .setClass(ExternalUserModel.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }
    //add shibo.zheng end

    /**
     * QQ登录
     * outway=json                                  //输出
     * AccessToken=E0775D2722EFD065BD3AD09E2D4BF4F1 //令牌
     */
    public static void qqLogin(String accessToken, Consumer<ExternalUserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_QQLOGIN)
                .addParams("outway", "json")
                .addParams("SysType", "Android")
                .addParams("AccessToken", accessToken)
                .setObject(true)
                .setClass(ExternalUserModel.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }


    /**
     * 提交终端设备号
     *
     * @param consumer dtype=iPhone                             //设备类型
     *                 dwsinf=四核16G内存600G硬盘               //操作系统
     *                 otype=5.0.1                              //操作系统
     *                 dtoken=b5bc082e81                        //设备编号
     *                 appname=教师端1.2                        //程序名称
     *                 marker=发现可用空间太小建议清理。        //其它信息
     */
    public static void postDeviceInfo(String token, Map<String, Object> fieldMap, Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.POSTDEVICEINFO)
                .addParams("token", token)
                .putField(fieldMap)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取用户信息
     * <p>
     * cmd=GetUserInf
     * token=        //令牌或
     * usersid=      //客户sid
     */
    public static void tokenGetUserInfo(String token, String userSid, Consumer<UserModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AMAPI_SIMPLEPLANCMD)
                .addParams("cmd", "GetUserInf")
                .addParams("token", token)
                .addParams("usersid", userSid)
                .setObject(true)
                .setClass(UserModel.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }
}
